package cn.iocoder.yudao.module.system.controller.admin.mini.goodInfo.goods.util;

import java.io.File;
import java.io.FileOutputStream;

import cn.iocoder.yudao.module.system.controller.admin.mini.other.file.vo.MultipartFileToFile;
import cn.iocoder.yudao.module.system.controller.admin.mini.other.file.vo.OssUtil;
import cn.iocoder.yudao.module.system.controller.admin.mini.goodInfo.supplier.controller.admin.supplier.MatrixToImageWriter;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;

public class BarcodeGenerator {

    public static String getUrl(String id){

//        String barcodeData = "1234567890"; // 要生成条形码的数据

        String result="";

        try {
            int width = 300; // 条形码的宽度
            int height = 100; // 条形码的高度

            MultiFormatWriter writer = new MultiFormatWriter();
            BitMatrix bitMatrix = writer.encode(id, BarcodeFormat.CODE_128, width, height);



            File outputFile = new File("barcode.png"); // 生成的条形码图片文件
            MatrixToImageWriter.writeToStream(bitMatrix, "png", new FileOutputStream(outputFile));

            result = OssUtil.uploadFileToOss(outputFile);
            MultipartFileToFile.deleteTempFile(outputFile);
            outputFile.delete();

            System.out.println("条形码已生成并保存至：" + outputFile.getAbsolutePath());
            System.out.println("result：" + result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static void main(String[] args) {
        String url = getUrl("123456");
    }
}